/****************************************************************************
*  Copyright 2021 Gorgon Meducer (Email:embedded_zhuoran@hotmail.com)       *
*                                                                           *
*  Licensed under the Apache License, Version 2.0 (the "License");          *
*  you may not use this file except in compliance with the License.         *
*  You may obtain a copy of the License at                                  *
*                                                                           *
*     http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                           *
*  Unless required by applicable law or agreed to in writing, software      *
*  distributed under the License is distributed on an "AS IS" BASIS,        *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
*  See the License for the specific language governing permissions and      *
*  limitations under the License.                                           *
*                                                                           *
****************************************************************************/

/* Includes --------------------------------------------------------------------------------*/
#include <stdint.h>
#include <stdio.h>
#include <cmsis_compiler.h>
#include "perf_counter.h"
#include "common.h"

/* Define ----------------------------------------------------------------------------------*/
/* Macro -----------------------------------------------------------------------------------*/
/* Typedef ---------------------------------------------------------------------------------*/
/* Variables -------------------------------------------------------------------------------*/
/* Prototypes ------------------------------------------------------------------------------*/
/* Functions -------------------------------------------------------------------------------*/
void printf_hex(char *title, unsigned char *hex, int n);
	

/**
 * @brief  Main function
 * @param  
 * @retval 
 */
int main (void) 
{
	unsigned char p1[256];
	unsigned char p2[512];
	unsigned char p3[512];
	int i, len1, len2, len3;

    while (1) 
		{
			printf("\r\nhello world\r\n");
			
			len1 = sizeof(p1)/sizeof(p1[0]);
			len2 = sizeof(p2)/sizeof(p2[0]);
			len3 = sizeof(p3)/sizeof(p3[0]);
			
			for(i = 0; i < len1; i++)
			{
				p1[i] = i % 256;
			}

			printf_hex("Before escape::\r\n", p1, len1);
			printf("Before Encode, len1: %d\r\n", len1);

			//escape
			mark_7e_encode(p1, len1, p2, &len2);		

			printf_hex("After escape::\r\n", p2, len2);
			printf("After Encode, len2: %d\r\n", len2);

			//unescape
			mark_7e_decode(p2, len2, p3, &len3);

			printf_hex("unescape::\r\n", p3, len3);
			printf("After Decode, len3: %d\r\n", len3);

			delay_us(1000000);
			return 0;
    }
}

/**
 * @brief  
 * @param  
 * @retval 
 */
void printf_hex(char *title, unsigned char *hex, int n)
{
    int i;

    printf("%s", title);
    for(i = 0; i < n; i++)
    {
        if(i % 16 == 0 && i != 0)
            printf("\r\n");
        printf("%02x ", (unsigned char )hex[i]);
    }
    printf("\r\n");
}
